package com.lizhi.parse;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.lizhi.model.ArticleModel;
import com.lizhi.util.Util;

public class ParseArticle extends Thread {
	private ArticleModel mArticleModel;

	public ParseArticle(ArticleModel articleModel) {
		mArticleModel = articleModel;
	}

	@Override
	public void run() {

		while (true) {

			try {
				System.out.println(mArticleModel.href);
				Document document = Jsoup.connect(mArticleModel.href).get();
				Elements elements = document.getElementsByClass("PostContent").get(0).getElementsByTag("p");
				StringBuilder builder = new StringBuilder();
				if (elements.size() <= 1) {
					String temp = elements.text();
					builder.append(temp);
				} else {
					for (int i = 0; i < elements.size(); i++) {
						builder.append(elements.get(i).text());
						builder.append("\n");
					}

				}
//				System.out.println(builder);
				Util.upload("https://freebook.firebaseio.com/articles.json", JSON.toJSONString(mArticleModel));
				String contentUrl = "https://freebook.firebaseio.com/content/articles/" + mArticleModel.type + "/" + mArticleModel.id + ".json";
				System.out.println(contentUrl);
				Util.uploadPut(contentUrl, JSON.toJSONString(builder.toString()));
				break;
			} catch (Exception e) {
				// Util.saveFile(mChapterModel.name, mHref);
				e.printStackTrace();
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				}
			}

		}

	}

	public static void main(String[] args) {
		// new ParseArticle("http://www.lz13.cn/lizhiwenzhang/98633.html").start();
	}
}
